import java.util.List;

public class UpdateTask {

	Task taskToBeUpdated;
	Task updatedTask;
	public UpdateTask() {
		taskToBeUpdated = new Task();
		updatedTask = new Task();
	}

	/**
	 * This method is called to execute the update command
	 * @param userInputElements
	 * @param tasksInTempStorage
	 * @param tasksOnDisplay
	 * @param typeTasksOnDisplay
	 */
	public void execute(UserInputElements userInputElements,
			List<Task> tasksInTempStorage, List<Task> tasksOnDisplay,
			String typeTasksOnDisplay) {
		String message;
		int numberOfTasksDisplayed = tasksOnDisplay.size();
		
		/*int indexOfUpdateLine = Integer.parseInt(userInputElements
				.getTaskDescription());
		boolean outOfBounds = ((indexOfUpdateLine <= 0) || (indexOfUpdateLine > numberOfTasksDisplayed));*/
		//TODO: line above - move down nicely after checking
		
		if(hasIncorrectUpdateArguments(userInputElements))
		{
			message = "incorrect arguments";
		}
		
		else if (Integer.parseInt(userInputElements.getTaskDescription()) <= 0 || Integer.parseInt(userInputElements
				.getTaskDescription()) > numberOfTasksDisplayed) {
			message = "out of bounds";
		}
		
		else {
			this.updateFromTempStorage(userInputElements, tasksInTempStorage, tasksOnDisplay, typeTasksOnDisplay);
			message = "updated from task list successfully";
		}
		
		TaskO.taskOMainScreen.appendOutput(message);
		System.out.println(message);
	}

	/**
	 * 
	 * @param userInputElements
	 * @return
	 */
	private boolean hasIncorrectUpdateArguments(
			UserInputElements userInputElements) {
		char[] parameters;
		parameters = userInputElements.getTaskDescription().toCharArray();
		boolean isIncorrectArgs = false;

		for (int i = 0; i < parameters.length; i++) {
			if (!Character.isDigit(parameters[i])) {
				isIncorrectArgs = true;
			}
		}
		return isIncorrectArgs;
	}
	
	/**
	 * 
	 * @param userInputElements
	 * @param tasksInTempStorage
	 * @param tasksOnDisplay
	 * @param typeTasksOnDisplay
	 */
	private void updateFromTempStorage(UserInputElements userInputElements,
			List<Task> tasksInTempStorage, List<Task> tasksOnDisplay,
			String typeTasksOnDisplay) {
		int taskNumberToBeUpdated = Integer.parseInt(userInputElements.getTaskDescription());
		taskToBeUpdated = tasksOnDisplay.get(taskNumberToBeUpdated-1);
		int taskId = taskToBeUpdated.getTaskId();
		
		updatedTask = this.generateUpdatedTask(userInputElements, taskToBeUpdated);
		this.updateTaskOnCurrentDisplay(tasksOnDisplay, taskId, updatedTask);
		this.updateTaskOnAllTaskList(tasksInTempStorage, taskId, updatedTask);
	}

	/**
	 * 
	 * @param userInputElements
	 * @param taskForUpdate
	 * @return
	 */
	private Task generateUpdatedTask(
			UserInputElements userInputElements, Task taskForUpdate) {
	
		if(userInputElements.getTaskTitle() != "") {
		taskForUpdate.setTaskTitle(userInputElements.getTaskTitle());	
		}
		
		if(userInputElements.getTaskStartDate() != "") {
			taskForUpdate.setTaskStartDate(userInputElements.getTaskStartDate());	
		}
		
		if(userInputElements.getTaskEndDate() != "") {
			taskForUpdate.setTaskEndDate(userInputElements.getTaskEndDate());	
		}
		
		if(userInputElements.getTaskDescription() != "") {
			taskForUpdate.setTaskDescription(userInputElements.getTaskDescription());	
		}
		
		if(userInputElements.getIsTaskStarred() != false) {
			taskForUpdate.setIsTaskStarred(userInputElements.getIsTaskStarred());	
		}
		
		if(!userInputElements.getTaskType().name().equalsIgnoreCase("UNDEFINED")) {
			String temp;
			temp = userInputElements.getTaskType().name();
			taskForUpdate.setTaskType(temp);	
		}
		
		if(userInputElements.getIsTaskDone() != false) {
			taskForUpdate.setIsTaskDone(userInputElements.getIsTaskDone());	
		}
		return taskForUpdate;
	}
	
	/**
	 * 
	 * @param tasksOnDisplay
	 * @param taskId
	 * @param taskForUpdate
	 */
	private void updateTaskOnCurrentDisplay(List<Task> tasksOnDisplay,
			int taskId, Task taskForUpdate) {
		
		for(int i=0; i<tasksOnDisplay.size(); i++) {
			if(tasksOnDisplay.get(i).getTaskId()==taskId) {
				tasksOnDisplay.remove(i);
				tasksOnDisplay.add(i, taskForUpdate);
			}
		}
	}

	/**
	 * 
	 * @param tasksInTempStorage
	 * @param taskId
	 * @param taskForUpdate
	 */
	private void updateTaskOnAllTaskList(List<Task> tasksInTempStorage,
			int taskId, Task taskForUpdate) {
		
		for(int i=0; i<tasksInTempStorage.size(); i++) {
			if(tasksInTempStorage.get(i).getTaskId()==taskId) {
				tasksInTempStorage.remove(i);
				tasksInTempStorage.add(i, taskForUpdate);
			}
		}
		
	}
	
}
